<?php

namespace app\api\controller\contact;

use think\Controller;

class Contact extends Controller
 {

    public function __construct()
 {
        parent::__construct();

        $this->ContactTypeModel = model( 'Contact.Type' );
        $this->UserModel = model( 'User.User' );
        $this->ContactModel = model( 'Contact.Contact' );
        $this->RegionModel = model( 'Region' );
    }

    // 分类

    public function type()
 {
        if ( $this->request->isPost() )
 {
            $result = $this->ContactTypeModel->select();
            $this->success( '分类查询成功', '', $result );
            exit;
        }
    }

    // 添加

    public function add()
 {
        if ( $this->request->isPost() )
 {
            $userid = $this->request->param( 'userid', 0 );
            $params = $this->request->param();

            $userinfo = $this->UserModel->find( $userid );

            //当没有找到用户的情况下
            if ( !$userinfo ) {
                $this->error( '用户不存在，请重新输入' );
                exit;
            }

            $region = $params[ 'region' ];

            //查询地区数据
            $parentpath = $this->RegionModel->where( [ 'code' => $region[ 2 ] ] )->value( 'parentpath' );

            if ( empty( $parentpath ) ) {
                $this->error( '您所选择的地区不存在,请重新选择' );
                exit;
            }

            $name = $this->ContactModel->where( [ 'nickname' => $params[ 'nickname' ], 'userid' => $userid ] )->select();

            if ( $name ) {
                $this->error( '该名字已经存,请重新输入' );
                exit;
            }
            $data = [
                'userid' => $userid,
                'nickname' => $params[ 'nickname' ],
                'typeid' => $params[ 'typeid' ],
                'phone' => $params[ 'phone' ],
                'gender' => $params[ 'gender' ],
                'region' => $parentpath,
                'remark' => $params[ 'remark' ]
            ];

            $result = $this->ContactModel->save( $data );

            if ( $result === FALSE )
 {
                $this->error( $this->ContactModel->getError() );
                exit;
            } else {
                $this->success( '新建成功' );
                exit;
            }
        }
    }

    // 首页列表

    public function index()
 {
        if ( $this->request->isPost() )
 {
            $keywords = $this->request->param( 'keywords', '' );
            $userid = $this->request->param( 'userid', 0 );
            $typeid = $this->request->param( 'typeid', 0 );

            $userinfo = $this->UserModel->find( $userid );

            //当没有找到用户的情况下
            if ( !$userinfo ) {
                $this->error( '用户不存在，请重新输入' );
                exit;
            }

            $where = [];

            if ( !empty( $keywords ) ) {
                $where[ 'nickname|phone' ] = [ 'like', "%$keywords%" ];
            }

            if ( $typeid > 0 ) {
                $where[ 'typeid' ] = $typeid;
            }

            $where[ 'userid' ] = $userid;

            $result = $this->ContactModel->where( $where )->select();

            if ( $result === FALSE )
 {
                $this->error( $this->ContactModel->getError() );
                exit;
            } else {
                $this->success( '查询成功', '', $result );
                exit;
            }
        }
    }

    // 详情

    public function info()
 {
        if ( $this->request->isPost() )
 {
            $id = $this->request->param( 'id', 0 );
            $userid = $this->request->param( 'userid', 0 );

            $userinfo = $this->UserModel->find( $userid );

            //当没有找到用户的情况下
            if ( !$userinfo ) {
                $this->error( '用户不存在，请重新输入' );
                exit;
            }

            $result = $this->ContactModel->with( 'type' )->find( $id );

            if ( $result === FALSE )
 {
                $this->error( $this->ContactModel->getError() );
                exit;
            } else {
                $this->success( '查询详情成功', '', $result );
                exit;
            }

        }
    }

    // 修改

    public function update()
 {
        if ( $this->request->isPost() )
 {
            $id = $this->request->param( 'id', 0 );
            $params = $this->request->param();
            $userid = $this->request->param( 'userid', 0 );

            $userinfo = $this->UserModel->find( $userid );

            //当没有找到用户的情况下
            if ( !$userinfo ) {
                $this->error( '用户不存在，请重新输入' );
                exit;
            }

            $info = $this->ContactModel->find( $id );

            if ( !$info ) {
                $this->error( '当前通讯录人员不存在，请新建通讯录' );
                exit;
            }

            $region = $params[ 'region' ];

            //查询地区数据
            $parentpath = $this->RegionModel->where( [ 'code' => $region[ 2 ] ] )->value( 'parentpath' );

            if ( empty( $parentpath ) ) {
                $this->error( '您所选择的地区不存在,请重新选择' );
                exit;
            }

            $data = [
                'id' => $id,
                'userid' => $userid,
                'nickname' => $params[ 'nickname' ],
                'typeid' => $params[ 'typeid' ],
                'phone' => $params[ 'phone' ],
                'gender' => $params[ 'gender' ],
                'region' => $parentpath,
                'remark' => $params[ 'remark' ]
            ];

            $result = $this->ContactModel->isUpdate()->save( $data );

            if ( $result === FALSE )
 {
                $this->error( $this->ContactModel->getError() );
                exit;
            } else {
                $this->success( '修改成功' );
                exit;
            }
        }
    }

    // 删除

    public function del()
 {
        if ( $this->request->isPost() )
 {
            $id = $this->request->param( 'id', 0 );
            $userid = $this->request->param( 'userid', 0 );

            $userinfo = $this->UserModel->find( $userid );

            //当没有找到用户的情况下
            if ( !$userinfo ) {
                $this->error( '用户不存在，请重新输入' );
                exit;
            }

            $info = $this->ContactModel->find( $id );

            if ( !$info ) {
                $this->error( '当前通讯录人员不存在，请新建通讯录' );
                exit;
            }

            $result = $this->ContactModel->destroy( $id );

            if ( $result === FALSE )
 {
                $this->error( $this->ContactModel->getError() );
                exit;
            } else {
                $this->success( '删除成功' );
                exit;
            }
        }
    }
}